﻿package net.wangit.dev.table;

import net.wangit.context.ActionContext;
import net.wangit.dev.DomainAction;
import net.wangit.dev.ObjectChecker;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.Type;
import net.wangit.jdbc.table.TableCache;
import net.wangit.util.Checker;

public class DeleteTableAction extends DomainAction {

	private static final String SQL_CHECK = "select l from TABLE where t=?";
	private static final String SQL_TABLE = "delete from TABLE where t=?";
	private static final String SQL_COLUMN = "delete from COLUMN where t=?";

	public DeleteTableAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		String tableName = ac.getRequestParameter("tablename");
		if (!Checker.isEmpty(tableName)) {
			net.wangit.MapList list = db.query(DeleteTableAction.SQL_CHECK,
					tableName, Type.VARCHAR);
			if (ObjectChecker.invalid(ac, list, "l"))
				return;
			db.execute(DeleteTableAction.SQL_COLUMN, tableName, 2);
			db.execute(DeleteTableAction.SQL_TABLE, tableName, 2);
			TableCache.clear(domain, tableName);
			TableReferenceUtil.update(db, domain, tableName, "");
		}
	}
}
